//
//  UIColor+Extension.swift
//  直播架构
//
//  Created by 我演示 on 2019/10/30.
//  Copyright © 2019 ALLSall. All rights reserved.
//




import UIKit

extension UIColor {

//    let co = UIColor.init(red: <#T##CGFloat#>, green: <#T##CGFloat#>, blue: <#T##CGFloat#>, alpha: <#T##CGFloat#>)
    /// 自定义信息
    convenience init(rg: CGFloat , g : CGFloat , b : CGFloat , alpha : CGFloat = 1.0 ){

        self.init(red: rg, green: g, blue: b, alpha: alpha)
    }

    /// hex 信息
    convenience init?(hex: String , alpha : CGFloat = 1.0) {

        guard hex.count >= 6  else {
            return nil
        }
        
        /**自定义处理信息*/
        
        ///2: 把字符串转化成大写
        var tempHex = hex.uppercased()
        
        ///3: 判断首字母是： 0x/ # /##
        if tempHex.hasPrefix("0#") || tempHex.hasPrefix("##") {
            tempHex = (tempHex as NSString ).substring(from: 2)
        }
        if tempHex.hasPrefix("#") {
            
            tempHex = (tempHex as NSString ).substring(from: 1)
        }
        
        /// 4: 分别取出rRGB
        var range = NSRange(location: 0, length: 2)
        
        let rHex = (tempHex as NSString).substring(with: range)
        range.location = 2
        
        let gHex = (tempHex as NSString).substring(with: range)
        range.location = 4
               
        let bHex = (tempHex as NSString).substring(with: range)
        
        /// 5:转换字符串转化为数字
        var r : UInt32 = 0 , g : UInt32 = 0 , b : UInt32 = 0
        Scanner(string: rHex).scanHexInt32(&r)
        Scanner(string: gHex).scanHexInt32(&g)
        Scanner(string: bHex).scanHexInt32(&b)
                    
        
        self.init(rg : CGFloat(r) , g : CGFloat(g) ,b : CGFloat(b))
    }
    class func randomColor() -> UIColor {
        return   UIColor(rg: CGFloat(arc4random_uniform(256)), g: CGFloat(arc4random_uniform(256)), b: CGFloat(arc4random_uniform(256)))
    }
    
    
}
